﻿@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@using SimpleIdServer.IdServer.Api.Provisioning;
@using SimpleIdServer.IdServer.Website.Stores.IdProviderStore;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.IdentityProvisioningStore;
@using System.Reflection;
@inject DialogService dialogService
@inject IState<UpdateIdentityProvisioningState> addIdProvisioningState
@inject NotificationService notificationService
@inject IDispatcher dispatcher

<RadzenSteps>
    <Steps>
        <RadzenStepsItem Text="@Global.MapperType">
            <div class="row gy-4">
                <!-- Choose a template -->
                <div>
                    <RadzenLabel Text="@Global.ChooseMappingRule" />
                    <RadzenDropDown @bind-Value=@selectedUsage
                                    TValue="IdentityProvisioningMappingUsage"
                                    Data="@mappingRuleUsages"
                                    Change=@(args => SelectUsage(args))
                                    TextProperty="Name" ValueProperty="Usage"></RadzenDropDown>
                </div>
                @foreach (var mappingRule in IdProvisioningMappingRules)
                {
                    <div class="col-md-6">
                        <RadzenCard @onclick="() => SelectMappingRule(mappingRule)" class="@(SelectedMappingRule != null && SelectedMappingRule.Name == mappingRule.Name ? "selected selectable" : "selectable")">
                            <div class="row">
                                <div class="col-md-3">
                                    <RadzenImage Path="@mappingRule.PictureUrl" Style="width: 60px" />
                                </div>
                                <div class="col">
                                    <h5>@mappingRule.Name</h5>
                                    <p class="text-muted">@(new MarkupString(mappingRule.ShortDescription))</p>
                                </div>
                            </div>
                        </RadzenCard>
                    </div>
                }
            </div>

            <hr />

            @if (SelectedMappingRule != null)
            {
                <h5><RadzenIcon Icon="info" /> @SelectedMappingRule.Name</h5>
                <p class="text-muted">@(new MarkupString(SelectedMappingRule.Description))</p>
            }
        </RadzenStepsItem>
        <RadzenStepsItem Text="Create" Disabled=@(SelectedMappingRule == null)>
            <EditIdProvisioningRule IsAdding=true Id="@IdProvisioningId" MappingRule="@idProvisioningMappingRule" IsUpdating="@addIdProvisioningState.Value.IsUpdating" Saved="@AddIdentityProvisioningMappingRule" />
        </RadzenStepsItem>
    </Steps>
</RadzenSteps>

@code {
    static ICollection<IdProvisioningMappingRule> AllIdProvisioningMappingRules = new List<IdProvisioningMappingRule>
    {
        new IdProvisioningMappingRule { Name = Global.Property, ShortDescription = Global.UserProperty, Description = Global.UserPropertyDescription, Type = MappingRuleTypes.USERPROPERTY, PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/UserProperty.png", Usage = IdentityProvisioningMappingUsage.USER },
        new IdProvisioningMappingRule { Name = Global.Claims, ShortDescription = Global.UserClaims, Description = Global.MapToOneUserClaim, Type = MappingRuleTypes.USERATTRIBUTE,  PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/UserAttribute.png", Usage = IdentityProvisioningMappingUsage.USER },
        new IdProvisioningMappingRule { Name = Global.Name, ShortDescription = Global.GroupName, Description = Global.SetGroupName, Type = MappingRuleTypes.GROUPNAME, PictureUrl = "_content/SimpleIdServer.IdServer.Website/images/UserProperty.png", Usage = IdentityProvisioningMappingUsage.GROUP }
    };

    [Parameter]
    public string IdProvisioningId { get; set; }

    record MappingRuleUsage
    {
        public IdentityProvisioningMappingUsage Usage { get; set; }
        public string Name { get; set; }
    }

    List<MappingRuleUsage> mappingRuleUsages = new List<MappingRuleUsage>
    {
        new MappingRuleUsage { Name = "User", Usage = IdentityProvisioningMappingUsage.USER },
        new MappingRuleUsage { Name = "Group", Usage = IdentityProvisioningMappingUsage.GROUP  }
    };

    IdentityProvisioningMappingUsage selectedUsage = IdentityProvisioningMappingUsage.USER;

    record IdProvisioningMappingRule
    {
        public string Name { get; set; } = null!;
        public string ShortDescription { get; set; } = null!;
        public string Description { get; set; } = null!;
        public MappingRuleTypes Type { get; set; }
        public string PictureUrl { get; set; } = null!;
        public IdentityProvisioningMappingUsage Usage { get; set; }
    }

    IdentityProvisioningMappingRuleResult idProvisioningMappingRule = new IdentityProvisioningMappingRuleResult();

    ICollection<IdProvisioningMappingRule> IdProvisioningMappingRules { get; set; } = AllIdProvisioningMappingRules.Where(id => id.Usage == IdentityProvisioningMappingUsage.USER).ToList();

    IdProvisioningMappingRule? SelectedMappingRule { get; set; } = null;

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        base.OnAfterRenderAsync(firstRender);
        if(firstRender)
        {
            SubscribeToAction<AddIdentityProvisioningMappingRuleSuccessAction>((act) =>
            {
                dialogService.Close();
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.IdProvisioningMappingRuleAdded });
                StateHasChanged();
            });
            SubscribeToAction<AddIdentityProvisioningMappingRuleFailureAction>((act) =>
            {
                dialogService.Close();
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
                StateHasChanged();
            });
        }

        return Task.CompletedTask;
    }

    void SelectMappingRule(IdProvisioningMappingRule mappingRule)
    {
        SelectedMappingRule = mappingRule;
        idProvisioningMappingRule.MapperType = mappingRule.Type;
    }

    void AddIdentityProvisioningMappingRule(IdentityProvisioningMappingRuleResult form)
    {
        dispatcher.Dispatch(new AddIdentityProvisioningMappingRuleAction
        {
            Id = IdProvisioningId,
            MappingRule = SelectedMappingRule.Type,
            From = form.From,
            TargetUserAttribute = form.TargetUserAttribute,
            HasMultipleAttribute = form.HasMultipleAttribute,
            Usage = selectedUsage
        });
    }

    void SelectUsage(object usage)
    {
        IdProvisioningMappingRules = AllIdProvisioningMappingRules.Where(id => id.Usage == (IdentityProvisioningMappingUsage)usage).ToList();
    }
}